package com.cn.mmd.controller;

import com.cn.mmd.model.Course;
import com.cn.mmd.model.CourseExample;
import com.cn.mmd.model.Student;
import com.cn.mmd.model.Teacher;
import com.cn.mmd.service.CourseService;
import com.cn.mmd.service.StudentService;
import com.cn.mmd.service.TeacherService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by Administrator on 2016/6/28 0028.
 */
@Controller
@RequestMapping(value = "page/poi")
public class PoiExport {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;

//    @RequiresPermissions("asdada")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public String exportExcel(HttpServletRequest request, HttpServletResponse response
    ,HSSFWorkbook wb,CourseExample courseExample)
            throws Exception {
        String o = request.getParameter("type").trim();

        if (o.equals("student")) {
            List<Student>  list = studentService.selectByAllEx("开除");
            wb = PoiExport.this.Export(list, o);
        }else if (o.equals("teacher")){
            List<Teacher> list= teacherService.selectByAllExT("离职");
            wb = PoiExport.this.Export(list, o);
        }else if (o.equals("course")){
            List<Course>list = courseService.selectByExample(courseExample);
            wb = PoiExport.this.Export(list,o);
        }
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=excel.xls");
        OutputStream ouputStream = response.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();

        return "/index.jsp";
    }

    private HSSFWorkbook Export(List list, String o) {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Sheet1");
        HSSFRow row = sheet.createRow((int) 0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        if (o.trim().equals("student")) {
            int[] excelHeaderWidth = {150, 90, 50, 180, 180, 180, 180, 90, 150, 150, 150, 90};
            String[] excelHeader = {"学生编号", "姓名", "性别", "身份证号", "所在学校", "所属学院", "学生专业", "学生年级", "学生班级", "手机号", "QQ邮箱", "备注"};
            for (int i = 0; i < excelHeaderWidth.length; i++) {
                sheet.setColumnWidth(i, 32 * excelHeaderWidth[i]);
            }
            for (int i = 0; i < excelHeader.length; i++) {
                HSSFCell cell = row.createCell(i);
                cell.setCellValue(excelHeader[i]);
                cell.setCellStyle(style);
            }
            Student student;
            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                student = (Student) list.get(i);
                row.createCell(0).setCellValue(student.getStudentid());
                row.createCell(1).setCellValue(student.getStudentname());
                row.createCell(2).setCellValue(student.getStudentsex());
                row.createCell(3).setCellValue(student.getStudentidcard());
                row.createCell(4).setCellValue(student.getStudentschool());
                row.createCell(5).setCellValue(student.getStudentcollege());
                row.createCell(6).setCellValue(student.getStudentmajor());
                row.createCell(7).setCellValue(student.getStudentclasss());
                row.createCell(8).setCellValue(student.getStudentclass());
                row.createCell(9).setCellValue(student.getStudentphonenumber());
                row.createCell(10).setCellValue(student.getStudentqqmail());
                row.createCell(11).setCellValue(student.getStudentremarks());
            }
        }else if (o.trim().equals("teacher")){
            int[] excelHeaderWidth = {150, 90, 50,180, 180, 180, 180, 100};
            String[] excelHeader = {"教师编号","教师姓名","教师性别","教师电话","教师身份证号","教师所属学院","教师所在学校","职务"};
            for (int i = 0; i < excelHeaderWidth.length; i++) {
                sheet.setColumnWidth(i, 32 * excelHeaderWidth[i]);
            }
            for (int i = 0; i < excelHeader.length; i++) {
                HSSFCell cell = row.createCell(i);
                cell.setCellValue(excelHeader[i]);
                cell.setCellStyle(style);
            }
            Teacher teacher;
            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                teacher = (Teacher) list.get(i);
                row.createCell(0).setCellValue(teacher.getTeacherid());
                row.createCell(1).setCellValue(teacher.getTeachername());
                row.createCell(2).setCellValue(teacher.getTeachersex());
                row.createCell(3).setCellValue(teacher.getTeacherphonenumber());
                row.createCell(4).setCellValue(teacher.getTeacheridcard());
                row.createCell(5).setCellValue(teacher.getTeachercollege());
                row.createCell(6).setCellValue(teacher.getTeacherschool());
                row.createCell(7).setCellValue(teacher.getTeacherposition());
            }
        }else if (o.equals("course")){
            int[] excelHeaderWidth = {150, 90, 90, 180, 180, 500, 300, 90, 90, 150, 300, 90};
            String[] excelHeader = {"课程编码","课程名","起止周格式 01-14","学分","开课学院"
                    ,"上课时间 例 周日第7,8节{第5-10周};周日第9节{第5-10周}",
                    "课程教室 例 D1-501;D1-502","场地要求","任教老师","任教老师工号",
                    "课程性质","教学班组成 例 安全20151,安全20152","学校"};
            for (int i = 0; i < excelHeaderWidth.length; i++) {
                sheet.setColumnWidth(i, 32 * excelHeaderWidth[i]);
            }
            for (int i = 0; i < excelHeader.length; i++) {
                HSSFCell cell = row.createCell(i);
                cell.setCellValue(excelHeader[i]);
                cell.setCellStyle(style);
            }
            Course course;
            for (int i = 0; i < list.size(); i++) {
                row = sheet.createRow(i + 1);
                course= (Course) list.get(i);
                row.createCell(0).setCellValue(course.getCourseid());
                row.createCell(1).setCellValue(course.getCoursename());
                row.createCell(2).setCellValue(course.getCoursebeginend());
                row.createCell(3).setCellValue(course.getCoursecredit());
                row.createCell(4).setCellValue(course.getCoursecollege());
                row.createCell(5).setCellValue(course.getCoursetime());
                row.createCell(6).setCellValue(course.getCourseclassroom());
                row.createCell(7).setCellValue(course.getCourserequest());
                row.createCell(8).setCellValue(course.getCourseteacher());
                row.createCell(9).setCellValue(course.getCourseteacherid());
                row.createCell(10).setCellValue(course.getCoursenature());
                row.createCell(11).setCellValue(course.getCourseclasss());
                row.createCell(12).setCellValue(course.getCoursecollegec());
            }
        }
        return wb;
    }
}
